package com.zch.house.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zch.house.entity.Lessee;
import com.zch.house.service.LesseeService;
import com.zch.house.service.impl.LesseeServiceImpl;
import com.zch.house.util.DateFormat;

/**
 * Servlet implementation class LesseeServlet
 */
@WebServlet("/LesseeServlet2")
public class LesseeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    LesseeService ls = new LesseeServiceImpl();   
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LesseeServlet() {
        super();
    }

    /**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String method = request.getParameter("method");
		if("add".equals(method)){
			add(request,response);
		}else if("delete".equals(method)){
			delete(request,response);
		}else if("update".equals(method)){
			update(request,response);
		}else if("select".equals(method)){
			select(request,response);
		}else if("glbi".equals(method)){
			getLesseeById(request,response);
		}
	}

	private void add(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String l_name = request.getParameter("name");
		String l_tel = request.getParameter("tel");
		String l_sex = request.getParameter("sex");
		String l_native_place = request.getParameter("nativePlace");
		String l_id_card = request.getParameter("idCard");
		System.out.println(l_name);
		String createTimeStr = request.getParameter("createTime");
		Date l_create_time = DateFormat.to_Date2(createTimeStr);
		System.out.println(l_create_time);
		String statusStr = request.getParameter("status");
		System.out.println(statusStr);
		int l_status = Integer.parseInt(statusStr);
		Lessee lessee = new Lessee(l_name, l_tel, l_sex, l_native_place, l_id_card, l_create_time, l_status);
		System.out.println(lessee.toString());
		//调用service层方法
		boolean flag = ls.addLessee(lessee);
		PrintWriter out = response.getWriter();
		if (flag) {
			// 添加成功,将添加成功的信息弹框
			out.print("<head><meta charset='utf-8'></head>");// 给弹框的内容设置编码方式
			out.println(
					"<script language='javascript'>alert('添加成功！');window.location.href='LesseeServlet2?method=select'</script>");
		} else {
			// 添加失败,将添加失败的信息弹框
			out.print("<head><meta charset='utf-8'></head>");// 给弹框的内容设置编码方式
			out.println(
					"<script language='javascript'>alert('添加失败！');window.location.href='LesseeServlet2?method=select'</script>");
		}
	}

	private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String idStr = request.getParameter("id");
		int id = Integer.parseInt(idStr);
		boolean flag = ls.deleteLessee(id);
		// 实例化一个out对象
		PrintWriter out = response.getWriter();
		if (flag) {
			// 删除成功,将删除成功的信息弹框
			out.print("<head><meta charset='utf-8'></head>");// 给弹框的内容设置编码方式
			out.println(
					"<script language='javascript'>alert('删除成功！');window.location.href='LesseeServlet2?method=select'</script>");
		} else {
			// 删除失败,将删除失败的信息弹框
			out.print("<head><meta charset='utf-8'></head>");// 给弹框的内容设置编码方式
			out.println(
					"<script language='javascript'>alert('删除失败！');window.location.href='LesseeServlet2?method=select'</script>");
		}

	}

	private void update(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String idStr = request.getParameter("id");
		int id = Integer.parseInt(idStr);
		String l_name = request.getParameter("name");
		String l_tel = request.getParameter("tel");
		String l_sex = request.getParameter("sex");
		String l_native_place = request.getParameter("nativePlace");
		String l_id_card = request.getParameter("idCard");
		String createTimeStr = request.getParameter("createTime");
		Date l_create_time = DateFormat.to_Date2(createTimeStr);
		String statusStr = request.getParameter("status");
		int l_status = Integer.parseInt(statusStr);
		Lessee lessee = new Lessee(id, l_name, l_tel, l_sex, l_native_place, l_id_card, l_create_time, l_status);
		boolean flag = ls.updateLessee(lessee);
		PrintWriter out = response.getWriter();
		if (flag) {
			// 修改成功,将修改成功的信息弹框
			out.print("<head><meta charset='utf-8'></head>");// 给弹框的内容设置编码方式
			out.println(
					"<script language='javascript'>alert('修改成功！');window.location.href='LesseeServlet2?method=select'</script>");
		} else {
			// 修改失败,将修改失败的信息弹框
			out.print("<head><meta charset='utf-8'></head>");// 给弹框的内容设置编码方式
			out.println(
					"<script language='javascript'>alert('修改失败！');window.location.href='LesseeServlet2?method=select'</script>");
		}
		
	}

	private void select(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String name = request.getParameter("name"); 
		String phone = request.getParameter("phone"); 
		String sex = request.getParameter("sex"); 
		String idCard = request.getParameter("idCard");
		String pageIndexStr = request.getParameter("pageIndex");
		if(pageIndexStr==null||pageIndexStr==""){
			pageIndexStr = "1";
		}
		int pageIndex = Integer.parseInt(pageIndexStr);
		//获取列表
		List<Lessee> list = ls.getAllLesseeByPage(name, phone, sex, idCard, pageIndex);
		//获得总页码
		//System.out.println(list);
		//System.out.println(list.get(0));
		//if(list.get(0).getL_sex() instanceof String){
			//System.out.println("性别为String类型");
		//}
		int totalPages = ls.getTotalCount(name, phone, sex, idCard);
		//将参数存入作用域
		request.setAttribute("name", name);
		request.setAttribute("phone", phone);
		request.setAttribute("sex", sex);
		request.setAttribute("idCard", idCard);
		request.setAttribute("pageIndex", pageIndex);
		request.setAttribute("list", list);
		request.setAttribute("totalPages", totalPages);
		//转发
		request.getRequestDispatcher("view/lessee/list.jsp").forward(request, response);
	}

	private void getLesseeById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String idStr = request.getParameter("id");
		int id = Integer.parseInt(idStr);
		Lessee lessee = ls.getLesseeById(id);
		request.setAttribute("lessee", lessee);
		request.getRequestDispatcher("view/lessee/edit.jsp").forward(request, response);		
	}

}
